<style>
    .rewritelist {
        padding: 20px 0;
        border-bottom: 1px solid #e6e8eb;
    }

    .rewritedet p {
        padding: 0 0 10px 0;
    }
</style>

<div class="rightcontent rc-bg-grey">

    <div class="rc-pagepadding" style="padding-bottom: 0;">

        <blockquote class="layui-elem-quote layui-quote-nm" style="background-color: #fff;margin-bottom:0">

            可自定义首页、列表页、文章页的url规则，cid为分类id，id为文章id ,规则中分别用{cid}、{id}来表示，编写好后需要自己在服务器中添加伪静态规则，否则无法生效
            <br />
            nginx范例：rewrite ^/app_cms_cate/(\d+) /app/cms/home/Index/category?cid=$1 last;

        </blockquote>

    </div>

    <div class="rc-pagepadding" style="padding-bottom: 0;">
        <div class=" rc-pannel ">
            <div class="rc-pannel-header layui-form">
                <div class="toolbar rc-bold ">自定义规则</div>
            </div>
            <div class="rc-pannel-body rc-pagepadding layui-form">
                <table class="layui-table">
                    <colgroup>
                        <col>
                        <col>
                        <col>
                        <col>
                        <col style="width: 280px;">
                    </colgroup>
                    <thead>
                        <tr>
                            <th>首页</th>
                            <th>列表页</th>
                            <th>列表分页</th>
                            <th>文章页</th>
                            <th>搜索页</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        foreach ($rewrites as $rewrite) {
                        ?>
                            <tr>
                                <td><?php echo $rewrite['index'] ?></td>
                                <td><?php echo $rewrite['cate'] ?></td>
                                <td><?php echo $rewrite['catepage'] ?></td>
                                <td><?php echo $rewrite['page'] ?></td>
                                <td><?php echo $rewrite['search'] ?></td>
                                <td class="rc-opbar">
                                    <input type="checkbox" name="use" title="启用|禁用" lay-skin="switch" value="<?php echo $rewrite['id']; ?>" <?php echo $rewrite['use'] == 1 ? 'checked' : '' ?> autocomplete="off" lay-filter="use">


                                    <a class="error delete" ruleid="<?php echo $rewrite['id'] ?>" lay-event="delete"><i class="iconfont">&#xe607;</i> 删除规则 </a>




                                </td>

                            </tr>
                        <?php
                        }
                        ?>

                        <tr>
                            <td>
                                <input type="text" id="index" class="layui-input" />
                            </td>
                            <td>
                                <input type="text" id="cate" class="layui-input" />
                            </td>
                            <td>
                                <input type="text" id="catepage" class="layui-input" />
                            </td>
                            <td>
                                <input type="text" id="page" class="layui-input" />
                            </td>
                            <td>
                                <input type="text" id="search" class="layui-input" />
                            </td>
                            <td>


                                <a href="javascript:;" class="layui-btn layui-btn-primary layui-btn-sm" id="add"><i class="iconfont">&#xe60e;</i> 添加规则</a>


                            </td>
                        </tr>
                    </tbody>
                </table>

            </div>
        </div>
    </div>





</div>


<script type='text/javascript'>
    layui.use(function() {

        var $ = layui.$;
        var form = layui.form;

        form.on('switch(use)', function(data) {
            var elem = data.elem; // 获得 checkbox 原始 DOM 对象
            var checked = elem.checked; // 获得 checkbox 选中状态
            var value = elem.value; // 获得 checkbox 值
            var othis = data.othis; // 获得 checkbox 元素被替换后的 jQuery 对象

            $.ajax({
                url: '/app/cms/admin/cnf/Rewrite/useRule', //请求路径
                type: "POST", //请求方式
                data: {
                    id: value,
                    use: (elem.checked ? 1 : 0)
                }, // 默认写法
                success: function(data) {
                    if (data.code == 0) {
                        toastr_success("设置成功", function() {
                            window.location.reload();
                        });
                    } else {
                        toastr_error(data.message);
                    }
                },
                error: function() {

                },

            });


        });

        $('#add').click(function() {
            var index = $('#index').val();
            var cate = $('#cate').val();
            var catepage = $('#catepage').val();
            var page = $('#page').val();
            var search = $('#search').val();
            $.ajax({
                url: '/app/cms/admin/cnf/Rewrite/addRule', //请求路径
                type: "POST", //请求方式
                data: {
                    index: index,
                    cate: cate,
                    catepage: catepage,
                    page: page,
                    search: search,
                }, // 默认写法
                success: function(data) {
                    if (data.code == 0) {
                        toastr_success("添加成功", function() {
                            window.location.reload();
                        });
                    } else {
                        toastr_error(data.message);
                    }
                },
                error: function() {

                },

            });

        })


        //删除规则
        $('.delete').click(function() {

            var ruleid = $(this).attr('ruleid');
            layer.confirm('确定要删除么？', {
                btn: ['确定', '关闭'] //按钮
            }, function() {
                $.ajax({
                    url: '/app/cms/admin/cnf/Rewrite/delRule', //请求路径
                    type: "POST", //请求方式
                    data: {
                        id: ruleid,
                    },
                    success: function(data) {
                        // datadicbox
                        if (data.code != 0) {
                            toastr_error(data.message);
                        } else {
                            window.location.reload();
                        }
                    },
                    error: function() {},
                    complete: function() {
                        layer.closeAll();
                    }
                });
            }, function() {


            });

        });

    });
</script>